#include "timer.h"
#include "asyncservice.h"
#include "timerfactory.h"
#include <boost/bind.hpp>

TimerFactory::TimerFactory(AsyncService &service) :
    m_service(service)
{

}

TimerFactory::~TimerFactory()
{

}

TimerFactory::TimerPtr TimerFactory::create(unsigned int owner,
                                            unsigned int id,
                                            unsigned long elapse,
                                            unsigned int repeat)
{
    TimerPtr _new;

    void *p = m_mp.alloc(sizeof(Timer));

    if(!p){
        return _new;
    }

    _new = TimerPtr
            (
                new(p) Timer(m_service.implement(), owner, id, elapse, repeat),
                boost::bind
                (
                    &TimerFactory::destroy,
                    this,
                    _1
                )
            );

    return _new;
}

void TimerFactory::destroy(Timer *p)
{
    if(!p){
        return;
    }

    p->~Timer();

    m_mp.free(p, sizeof(Timer));
}
